﻿/*
Copyright 2011 Shane Ng

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using freemarker.template;
using java.io;
using Freemarker.Net.MvcViewEngine.FreemarkerModel;
using System.Reflection;
using Freemarker.Net.Directive;

namespace Freemarker.Net.MvcViewEngine {
    public class FreemarkerView : IView{
        public class ViewDataContainer : IViewDataContainer {
            private ViewContext context;
            public ViewDataContainer(ViewContext context) {
                this.context = context;
            }

            public ViewDataDictionary ViewData {
                get {
                    return context.ViewData;
                }
                set {
                    context.ViewData = value;
                }
            }
        }

        private freemarker.template.Configuration config;
        private string viewPath;
        private IEnumerable<ImportableDirective> directives;
        public FreemarkerView(freemarker.template.Configuration config, string viewPath, IEnumerable<ImportableDirective> directives) {
            this.config = config;
            this.viewPath = viewPath;
            this.directives = directives;
        }

        public void Render(ViewContext viewContext, System.IO.TextWriter writer) {
            Template temp = config.getTemplate(viewPath.Substring(2));
            
            Dictionary<string, object> data = new Dictionary<string, object>{
                {"model", viewContext.ViewData.Model},
                {"session", viewContext.HttpContext.Session},
                {"http", viewContext.HttpContext},
                {"request", viewContext.HttpContext.Request},
                {"application", viewContext.HttpContext.Application},
                {"view", viewContext},
                {"controller", viewContext.Controller},
                {"url", new UrlHelper(viewContext.RequestContext)},
                {"html", new HtmlHelper(viewContext, new ViewDataContainer(viewContext))},
                {"ajax", new AjaxHelper(viewContext, new ViewDataContainer(viewContext))},
            };

            foreach (var directive in directives) {
                data.Add(directive.key, directive);
            }

            Writer output = new JavaTextWriter(writer);
            temp.process(data, output);
            output.flush();
        }
    }
}
